<?php

namespace CHOMP\SGIBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\FormInterface;
use CHOMP\SGIBundle\PermisoChomp\RolChomp;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;


class RolType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('rolNombre','text', array ('label' => 'Nombre',))
            ->add('rolDescripcion','textarea', array ('label' =>' ',))
            ->add('VYCActividad','choice', array(
                                    'label' => 'Actividad',
                                    'choices'   => array('1' => 'Ver', '3' => 'Crear'),
                                    'required'  => true,
                                    'multiple'  => false,
                                    'empty_data' => 'Ver',
                                    'attr'=> array('style'=>'width:100%')
                                    )
                )
            ->add('VYCEmpresa', 'entity', array (
                                                'label' => 'Empresa',
                                                'class' => 'CHOMPSGIBundle:Empresa',
                                                'query_builder' => function(EntityRepository $er){
                                                    //return $er->getAllActivas();
                                                    return $er->createQueryBuilder('u') 
                                                              -> where('u.borrado = 0')
                                                              -> orderBy('u.nombre', 'ASC');
                                                },
                                                'required'  => true,
                                                'empty_value' => 'Todas',
                                                'property' => 'nombre',
                                             )
                )
            /*
            ->add('Permisos_visualizacionyCreacionDeIncidentesString', 'textarea', array ('label' => 'Permisos_visualizacionyCreacionDeIncidentesString'));
            */

            /*$builder->addEventListener(
                FormEvents::PRE_SET_DATA,
                function (FormEvent $event) {
                    $form = $event->getForm();

                    // this would be your entity, i.e. SportMeetup
                    if ($form->get('VYCAgregar')->isClicked()) {
                        $rolChomp = $event->getData();
                        $rolChomp->getrolChomp() -> selectAndAddPermiso('VYC');
                    
                        //$positions = null === $sport ? array() : $sport->getAvailablePositions();

                        $form->add('Permisos_visualizacionyCreacionDeIncidentes', 'choice', array(
                            'empty_value' => '',
                            'choices'     => $rolChomp ->Permisos_visualizacionyCreacionDeIncidentes,
                        ));
                    }
                }
            );
            */
            /*$builder->add('Permisos_visualizacionyCreacionDeIncidentes','choice', array ('label' =>' ',
                                                                                )
                        )
             */       
            /*
            ->add('MDICampo')          
            ->add('MDIEmpresa')
            ->add('ABMActividad')
            ->add('ABMEntidad')
            ->add('Opcion')
            */
            ->add('save', 'submit', array('label' => 'Guardar'));
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'CHOMP\SGIBundle\PermisoChomp\RolChomp',
            'validation_groups' => 'false'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'chomp_sgibundle_permisoChomp';
    }
}
